﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace CoreInmobiliario.Frontend.HelperClasses
{
    public class ExportTo
    {
        
        public static void ExportarPublicacionPDF(int publicacionId, string path, string pathImageHeader)
        {
            Data.Publicacion publicacion = Business.Publicacion.GetInstance().GetById(publicacionId);

            Document document = new Document();
            PdfWriter writer = PdfWriter.GetInstance(document, new System.IO.FileStream(path, System.IO.FileMode.Create));
            PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, document.PageSize.Height, 1f); //Setea el zoom (1f = %100)
            document.Open();

            iTextSharp.text.Image header = iTextSharp.text.Image.GetInstance(pathImageHeader);
            header.Alignment = iTextSharp.text.Image.ALIGN_LEFT;
            document.Add(header);

            Font fontHeader = new Font(Font.FontFamily.UNDEFINED, 12, Font.BOLD, BaseColor.RED);
            Font fontTitle = new Font(Font.FontFamily.UNDEFINED, 10, Font.BOLD);
            Font fontDescription = new Font(Font.FontFamily.UNDEFINED, 10);

            #region Detalles Publicación
            document.Add(new Paragraph(new Chunk(publicacion.Operacion.Nombre + " - " + publicacion.Propiedad.Calle + " Nº" + publicacion.Propiedad.Numero + " - " + publicacion.Propiedad.Localidad.Nombre, fontHeader)));

            PdfPTable tableDetails = new PdfPTable(2);
            tableDetails.DefaultCell.Border = PdfPCell.NO_BORDER;
            tableDetails.WidthPercentage = 100;

            Paragraph paragraph = new Paragraph();
            Chunk chunkTitle = new Chunk("Precio: ", fontTitle);

            string precio = "";
            if (publicacion.OperacionId == 1)
                precio = publicacion.Moneda1.Signo + publicacion.PrecioVenta.ToString();
            else if (publicacion.OperacionId == 2)
                precio = publicacion.Moneda.Signo + publicacion.PrecioAlquiler.ToString();
            else
                precio = publicacion.Moneda1.Signo + publicacion.PrecioVenta.ToString() + " - " + publicacion.Moneda.Signo + publicacion.PrecioAlquiler.ToString();

            Chunk chunkDescription = new Chunk(precio, fontDescription);
            paragraph.Add(chunkTitle);
            paragraph.Add(chunkDescription);
            tableDetails.AddCell(paragraph);

            paragraph = new Paragraph();
            chunkTitle = new Chunk("Ambientes: ", fontTitle);
            chunkDescription = new Chunk(publicacion.Propiedad.Ambientes.ToString(), fontDescription);
            paragraph.Add(chunkTitle);
            paragraph.Add(chunkDescription);
            tableDetails.AddCell(paragraph);

            paragraph = new Paragraph();
            chunkTitle = new Chunk("Dormitorios: ", fontTitle);
            chunkDescription = new Chunk(publicacion.Propiedad.Dormitorios.ToString(), fontDescription);
            paragraph.Add(chunkTitle);
            paragraph.Add(chunkDescription);
            tableDetails.AddCell(paragraph);

            paragraph = new Paragraph();
            chunkTitle = new Chunk("Baños: ", fontTitle);
            chunkDescription = new Chunk(publicacion.Propiedad.Banio.ToString(), fontDescription);
            paragraph.Add(chunkTitle);
            paragraph.Add(chunkDescription);
            tableDetails.AddCell(paragraph);

            paragraph = new Paragraph();
            chunkTitle = new Chunk("Superficie: ", fontTitle);
            chunkDescription = new Chunk(publicacion.Propiedad.Superficie.ToString(), fontDescription);
            paragraph.Add(chunkTitle);
            paragraph.Add(chunkDescription);
            tableDetails.AddCell(paragraph);

            paragraph = new Paragraph();
            chunkTitle = new Chunk("Metros Cubiertos: ", fontTitle);
            chunkDescription = new Chunk(publicacion.Propiedad.MetrosCubiertos.ToString(), fontDescription);
            paragraph.Add(chunkTitle);
            paragraph.Add(chunkDescription);
            tableDetails.AddCell(paragraph);

            paragraph = new Paragraph();
            chunkTitle = new Chunk("Estado: ", fontTitle);
            chunkDescription = new Chunk(publicacion.Propiedad.Estado, fontDescription);
            paragraph.Add(chunkTitle);
            paragraph.Add(chunkDescription);
            tableDetails.AddCell(paragraph);

            paragraph = new Paragraph();
            chunkTitle = new Chunk("Antiguedad: ", fontTitle);
            chunkDescription = new Chunk(publicacion.Propiedad.Antiguedad, fontDescription);
            paragraph.Add(chunkTitle);
            paragraph.Add(chunkDescription);
            tableDetails.AddCell(paragraph);

            tableDetails.SpacingBefore = 10;
            document.Add(tableDetails);
            #endregion

            #region Características y Servicios
            PdfPTable tableCaracteristicaServicio = new PdfPTable(2);
            tableCaracteristicaServicio.DefaultCell.Border = PdfPCell.NO_BORDER;
            tableCaracteristicaServicio.WidthPercentage = 100;

            PdfPTable tableCaracteristicas = new PdfPTable(1);
            tableCaracteristicas.SpacingBefore = 10;
            tableCaracteristicas.WidthPercentage = 100;

            PdfPTable tableServicios = new PdfPTable(1);
            tableServicios.SpacingBefore = 10;
            tableServicios.WidthPercentage = 100;

            List<Data.Caracteristica> listCaracteristicas = Business.Caracteristica.GetInstance().GetListCaracteristicaByPropiedadId(publicacion.PropiedadId);
            List<Data.Servicio> listServicios = Business.Servicio.GetInstance().GetListServicioByPropiedadId(publicacion.PropiedadId);

            tableCaracteristicas.AddCell(new Paragraph("Características", fontTitle));
            tableServicios.AddCell(new Paragraph("Servicios", fontTitle));

            foreach (Data.Caracteristica c in listCaracteristicas)
            {
                PdfPCell cellCaracteristica = new PdfPCell(new Phrase(new Chunk(c.Nombre, fontDescription)));
                tableCaracteristicas.AddCell(cellCaracteristica);
            }

            tableCaracteristicaServicio.AddCell(tableCaracteristicas);

            foreach (Data.Servicio s in listServicios)
            {
                PdfPCell cellAccount = new PdfPCell(new Phrase(new Chunk(s.Nombre, fontDescription)));
                tableServicios.AddCell(cellAccount);
            }

            tableCaracteristicaServicio.AddCell(tableServicios);
            document.Add(tableCaracteristicaServicio);
            #endregion

            #region Requisitos y Descripción
            if (publicacion.Requisitos != "")
            {
                PdfPTable tableRequisitos = new PdfPTable(1);
                tableRequisitos.DefaultCell.Border = PdfPCell.NO_BORDER;
                tableRequisitos.WidthPercentage = 100;

                paragraph = new Paragraph();
                chunkTitle = new Chunk("Requisitos", fontTitle);
                chunkDescription = new Chunk(publicacion.Requisitos, fontDescription);
                paragraph.Add(chunkTitle);
                tableRequisitos.AddCell(paragraph);
                paragraph = new Paragraph();
                paragraph.Add(chunkDescription);
                tableRequisitos.AddCell(paragraph);

                document.Add(tableRequisitos);
            }

            if (publicacion.Propiedad.Descripcion != "")
            {
                PdfPTable tableDescripcion = new PdfPTable(1);
                tableDescripcion.DefaultCell.Border = PdfPCell.NO_BORDER;
                tableDescripcion.WidthPercentage = 100;

                paragraph = new Paragraph();
                chunkTitle = new Chunk("Descripción", fontTitle);
                chunkDescription = new Chunk(publicacion.Propiedad.Descripcion, fontDescription);
                paragraph.Add(chunkTitle);
                tableDescripcion.AddCell(paragraph);
                paragraph = new Paragraph();
                paragraph.Add(chunkDescription);
                tableDescripcion.AddCell(paragraph);

                document.Add(tableDescripcion);
            }
            #endregion

            PdfAction action = PdfAction.GotoLocalPage(1, pdfDest, writer);
            writer.SetOpenAction(action);

            document.Close();
        }


    }
}